package org.zpmis.api.http;

import org.zpmis.api.business.SysOrganizationWebService;
import org.zpmis.common.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.domain.org.qo.AddOrgQo;
import org.zpmis.domain.org.qo.ChangeOrgStatusQo;
import org.zpmis.domain.org.qo.EditOrgQo;
import org.zpmis.domain.org.qo.OrgListQo;
import org.zpmis.domain.org.vo.DeptTreeVo;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
* created with CodeGenerator
* description:
* @author  Yang
* date: 2021-6-2
* time: 14:45:18
*/
@RestController
@RequestMapping("/sys/organization")
@Slf4j
public class SysOrganizationController extends BaseController{

    @Resource
    private SysOrganizationWebService sysOrganizationWebService;

    /**
     * 组织列表查询
     * @param orgListQo
     * @return
     */
    @PostMapping("/sysOrgList")
    public PlatformResult orgList(@RequestBody OrgListQo orgListQo){
        try {
            return PlatformResult.success(sysOrganizationWebService.orgList(orgListQo));
        }catch (Exception e){
            log.error("[SysOrganizationController]---orgList--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 新增组织
     * @param addOrgQo
     * @return
     */
    @PostMapping("/addSysOrg")
    public PlatformResult addOrg(@Valid @RequestBody AddOrgQo addOrgQo){
        try {
            sysOrganizationWebService.addOrg(addOrgQo);
        } catch (Exception e) {
            log.error("[SysOrganizationController]---addOrg--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 修改组织信息
     * @param editOrgQo
     * @return
     */
    @PutMapping("/editSysOrg")
    public PlatformResult editOrg(@Valid @RequestBody EditOrgQo editOrgQo){
        try {
            sysOrganizationWebService.editOrg(editOrgQo);
        }catch (BusinessException e) {
            log.error("[SysOrganizationController]---editOrg--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysOrganizationController]---editOrg--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 禁用/启用组织
     * @param changeOrgStatusQo
     * @return
     */
    @PostMapping("/changeSysOrgStatus")
    public PlatformResult changeOrgStatus(@Valid @RequestBody ChangeOrgStatusQo changeOrgStatusQo){
        try {
            sysOrganizationWebService.changeOrgStatus(changeOrgStatusQo);
        }catch (BusinessException e) {
            log.error("[SysOrganizationController]---changeOrgStatus--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysOrganizationController]---changeOrgStatus--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    @PostMapping("allSysOrg")
    public PlatformResult allSysOrg(){
        try {
            return PlatformResult.success(sysOrganizationWebService.allSysOrg());
        } catch (BusinessException e) {
            log.error("[SysOrganizationController]---allSysOrg--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysOrganizationController]---allSysOrg--->Exception:",e);
            return PlatformResult.failure();
        }
    }
}
